﻿/*
版权所有：版权所有(C) 2011，刘建新
文件编号：M01_CouponusedBll.cs
文件名称：CouponusedBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2011-6-11
作　　者：
内容摘要：表[CouponUsed]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.Bus;
using System.Data;
using Eis51.CampanyCMS.Access.Bus;
using System.Collections;

namespace Eis51.CampanyCMS.Business.Bus
{
    /// <summary>
	/// Couponused的BLL 自定义方法
	/// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
	/// </summary>
    public partial class CouponusedBLL : BaseBLL<Couponused>
    {


        public bool  Submit(Couponused c )
        {
            if (c.Couponusedid > 0)
            {
                return this.Update(c) > 0;
            }
            else
            {
                return this.Insert(c) > 0;
            }
        }

        public bool OrderCoup( OrderApply obj , Coupon co)
        {
            string coupon = co.Couponno;
            Couponused c = GetCouponused(coupon, obj.ResUserNo);
            if (c == null )
            {
                if (co.Pcount <= 0) return false;
                c = new Couponused();
                c.Amount =Convert.ToDouble( co.Amount);
                c.Couponno = coupon;
                c.Orderno = obj.OrderNo;
                c.Username = obj.ResUserNo;
                 bool ireturn=   CouponusedBLL.GetInstance().Insert(c)>0;
                 co.Pcount = co.Pcount - 1;
                 CouponBLL.GetInstance().Update(co);
                 return ireturn;
            }
            else
            {
                c.Amount = Convert.ToDouble( co.Amount);
                c.Orderno = obj.OrderNo;
                return  CouponusedBLL.GetInstance().Update(c) >0;
            }
        }

        public void DeleteCouponused(int  Couponusedid)
        {
            Couponused c = this.Get(new Couponused.Key(Couponusedid));
            if (c != null)
            {
              Coupon c1=  CouponBLL.GetInstance().GetCoup(c.Couponno);
              if (c1 != null)
              {
                  c1.Pcount = c1.Pcount + 1;
                  CouponBLL.GetInstance().Update(c1);
              }
               this.Delete(c);
            }
        
        
        }


        public void DeleteByCouponNo(string CouponNo)
        {
            string sql = string.Format("delete  from CouponUsed where CouponNo='{0}'  ",CouponNo);
            this.Execute(sql);
        }

        public Couponused GetCouponused(string CouponNo)
        {
            string sql = string.Format("select *  from CouponUsed where CouponNo='{0}' ", CouponNo);
            DataTable dt = this.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                return Couponused.Dr2Obj(dt.Rows[0], Couponused.ColumnNameEnum.DBName);
            }
            return null;

        }


        public Couponused GetCouponused(string CouponNo, string UserName)
        {
            string sql = string.Format("select *  from CouponUsed where CouponNo='{0}' and UserName='{1}' ", CouponNo, UserName);
            DataTable dt = this.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                return Couponused.Dr2Obj(dt.Rows[0], Couponused.ColumnNameEnum.DBName);
            }
            return null;

        }

        public DataTable GetCouponusedData(string CouponNo)
        {
             string sql = string.Format("select *  from CouponUsed where CouponNo='{0}'  ", CouponNo);
            DataTable dt = this.GetDataTable(sql);
            return dt;
        }

        public DataTable GetCouponUsedData(string UserNo)
        {
            string sql = string.Format("select a.CouponNo,a.Orderno,a.Amount,b.BeginDate,b.EndDate,iif (b.Status=1,'有效','无效') as statusname from CouponUsed as a inner join Coupon as b on a.CouponNo=b.CouponNo where a.UserName='{0}'", UserNo);
            DataTable dt = this.GetDataTable(sql);
            return dt;
        }

        public int MyCoupCount(string UserNo)
        {
            string sql = string.Format(@"select count(*) as tcount from CouponUsed as a
                                                    inner join Coupon as b on a.CouponNo=b.CouponNo where a.UserName='{0}'
                                                    and b.status>0 and b.EndDate>= #{1}# and b.BeginDate<= #{1}# ", UserNo,DateTime.Now);
            DataTable dt = this.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
                return int.Parse(dt.Rows[0]["tcount"].ToString());
            return 0;
        
        }
    }

    #region 代码自动生成
    /// <summary>
    /// Couponused的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class CouponusedBLL : BaseBLL<Couponused>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: CouponusedBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected CouponusedBLL()
        {
        }
        private static volatile CouponusedBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 CouponusedBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static CouponusedBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(CouponusedBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new CouponusedBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>Couponused</returns>
        public Couponused Get(Couponused.Key key)
        {

            return CouponusedDao.GetInstance().SelectByID(key);
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList Select()
        {
            return CouponusedDao.GetInstance().SelectAll();
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(Couponused obj)
        {
            int result = 0;
            try
            {
                result = CouponusedDao.GetInstance().Insert(obj);
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(Couponused obj)
        {
            CouponusedDao.GetInstance().Update(obj);
            return 1;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(Couponused obj)
        {
            bool result = CouponusedDao.GetInstance().DeleteByID(new Couponused.Key(obj.Couponusedid));
            return result ? 1 : 0;
        }

        #endregion

    }
    #endregion
}